package com.zhouyu.gamecommunity.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhouyu.gamecommunity.dao.CommonMapper;
import com.zhouyu.gamecommunity.entity.Game;
import com.zhouyu.gamecommunity.entity.Message;
import com.zhouyu.gamecommunity.entity.PageData;
import com.zhouyu.gamecommunity.entity.Result;
import com.zhouyu.gamecommunity.service.CommonService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private CommonMapper commonMapper;

    @Override
    @Cacheable(value = "verifyCode", key = "#userId")
    public String createVerifyCode(String userId) {
        //随机生成一个六位数字
        Random random = new Random();
        return String.format("%06d", random.nextInt(1000000));
    }

    @Override
    public Result<Integer> attentionGame(String userId, String gameId) {
        return Result.success(commonMapper.attentionGame(userId, gameId));
    }

    @Override
    public Result<Integer> deleteAttention(String userId, String gameId) {
        return Result.success(commonMapper.deleteAttention(userId, gameId));
    }

    @Override
    public Result<PageData<JSONObject>> getGameComment(JSONObject params) {
        params.put("offset",params.getInteger("size") * (params.getInteger("page") - 1));
        List<JSONObject> commentList = commonMapper.getGameComment(params);
        PageData<JSONObject> pageData = new PageData<>();
        pageData.setList(commentList);
        pageData.setTotal(commonMapper.countComment(params));
        pageData.setPageNum(params.getInteger("page"));
        pageData.setPageSize(params.getInteger("size"));
        return Result.success(pageData);
    }

    @Override
    public Result<Integer> countComment(JSONObject params) {
        return Result.success(commonMapper.countComment(params));
    }

    @Override
    public Result<Integer> addComment(JSONObject params) {
        return Result.success(commonMapper.addComment(params));
    }

    @Override
    public Result<Integer> addArticle(JSONObject params) {
        String id = IdUtil.getSnowflake().nextIdStr();
        params.put("id", id);
        commonMapper.addArticleBanner(params);
        commonMapper.addArticleContent(params);
        return Result.success(commonMapper.addArticle(params));
    }

    @Override
    public Result<List<JSONObject>> getArticle(JSONObject params) {
        List<JSONObject> commentList = commonMapper.getArticle(params);
        return Result.success(commentList);
    }

    @Override
    public Result<List<JSONObject>> getArticleContent(String id) {
        return Result.success(commonMapper.getArticleContent(id));
    }

    @Override
    public Result<List<JSONObject>> getArticleBanner(String id) {
        return Result.success(commonMapper.getArticleBanner(id));
    }

    @Override
    public Result<List<JSONObject>> getUserAttentionList(String id) {
        return Result.success(commonMapper.getUserAttentionList(id));
    }

    @Override
    public Result<List<JSONObject>> getTopicList(JSONObject params) {
        return Result.success(commonMapper.getTopicList(params));
    }

    @Override
    public Result<Integer> subscribeTopic(String userId, String topicId) {
        return Result.success(commonMapper.subscribeTopic(userId, topicId));
    }

    @Override
    public Result<Integer> deleteSubscribe(String userId, String topicId) {
        return Result.success(commonMapper.deleteSubscribe(userId, topicId));
    }

    @Override
    public Result<Integer> likeObject(JSONObject params) {
        if (params.getBoolean("like")) {
            commonMapper.likeObject(params);
        }else {
            commonMapper.dislikeObject(params);
        }
        return Result.success();
    }

    @Override
    public Result<List<Game>> searchGame(JSONObject params) {
        return Result.success(commonMapper.searchGame(params));
    }

    @Override
    public Result<List<JSONObject>> getUpList(JSONObject params) {
        return Result.success(commonMapper.getUpList(params));
    }

    @Override
    public Result<Integer> attentionUser(JSONObject params) {
        if (params.getBoolean("attention")) {
            commonMapper.attentionUser(params);
            commonMapper.followAdd(params);
        }else {
            commonMapper.noAttentionUser(params);
            commonMapper.followDelete(params);
        }
        return Result.success();
    }
}
